/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.tienlen;

import extension.room.analyse.CardSet;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author daz
 */
public class SouthTienlenGame extends TienlenGame {

    private int ratioChange = 0;

    public SouthTienlenGame(SouthTienlenExtension tienlenExtension, int roomId) {
        super(tienlenExtension, roomId);

    }

    @Override
    public boolean checkCard(List<Integer> listCards) {
        int[] cardCheck = new int[listCards.size()];
        int count = 0;
        for (Integer card : listCards) {
            cardCheck[count] = card;
            count++;
        }
        //check hop le
        CardSet set = null;
        boolean isSetValid = true;
        if (count >= 2) {
            if (count >= 6) {
                set = analyser.extractSpecialThreePairs(cardCheck);
            }
            if (set == null) {
                set = analyser.extractSpecialFlush(cardCheck);
            }
            if (set == null) {
                set = analyser.extractSpecialPair(cardCheck);
            }
            if (set == null) {
                isSetValid = false;
            } else if (set.getSetSize() < count) {
                isSetValid = false;
            }
        }


        if (!isSetValid) {
            return false;
        }

        if (lastBlock
                != -1) {
            //check cung loai
            boolean isSameType = false;
            if (count == 1 && currentSet == null && currentCard != -1) {
                isSameType = true;
            } else if (currentSet != null && count == currentSet.getSetSize()) {
                isSameType = true;
            } else if (count == 4 && set != null && set.getType() == CardSet.PAIR && currentCard % 13 == 1) {
                isSameType = true;
                isChargePenalty = true;
            } else if (count >= 6 && set != null && set.getType() == CardSet.THREE_PAIR) {
                isSameType = true;
                isChargePenalty = true;
                if (currentSet == null && currentCard % 13 == 1) {
                    if (currentCard / 13 < 2) {
                        ratioChange = 4;
                    } else {
                        ratioChange = 3;
                    }
                } else {
                    ratioChange = 4;
                }
            }


            if (!isSameType) {
                return false;
            }

            boolean isBigger = false;
            if (set != null) {
                if (currentSet != null) {
                    isBigger = set.isBiggerSpecial(currentSet);
                    if (isBigger && set.getType() == CardSet.PAIR && set.getSetSize() == 4
                            && currentSet.getType() == CardSet.PAIR
                            && currentSet.getSetSize() == 4) {
                        isChargePenalty = true;
                    }
                } else if (currentCard % 13 == 1) {
                    if (set.getType() == CardSet.PAIR && set.getSetSize() == 4) {
                        isBigger = true;
                    } else if (set.getType() == CardSet.THREE_PAIR) {
                        isBigger = true;
                    }
                }
            } else {
                isBigger = (cardCheck[0] - 2 + 13) % 13 > (currentCard - 2 + 13) % 13
                        || ((cardCheck[0] - 2 + 13) % 13 == (currentCard - 2 + 13) % 13
                        && cardCheck[0] / 13 < currentCard / 13);
                if (cardCheck[0] % 13 == 1 && currentCard % 13 == 1 && cardCheck[0] / 13 < currentCard / 13) {
                    isBigger = true;
                }

            }

            if (!isBigger) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean checkRightRule(List<Integer> listCards) {
        return false;
    }

    @Override
    protected List<Integer> getListBlock() {
        List<Integer> listBlock = new LinkedList<Integer>();
        if (lastBlock != -1) {
            if (currentCard != -1) {
                int cardChoose = listPlayers[gameTurn].getSuggestCard2(currentCard);
                if (cardChoose != -1) {
                    listBlock.add(cardChoose);
                } else if (currentCard % 13 == 1) {
                    listBlock.addAll(listPlayers[gameTurn].getSuggestFourOfKind());
                }
            } else if (currentSet != null) {
                List<Integer> listSuggest = listPlayers[gameTurn].getSuggestCard2(currentSet);
                listBlock.addAll(listSuggest);
            }
        } else {
            int cardChoose = listPlayers[gameTurn].getSugesstMinCard();
            listBlock.add(cardChoose);
        }
        return listBlock;
    }

    @Override
    protected void extractSet(int[] cardCheck) {
        currentSet = analyser.extractSpecialFlush(cardCheck);
        if (currentSet == null) {
            currentSet = analyser.extractSpecialPair(cardCheck);
        }
    }

    @Override
    protected long getMoneyChange() {
        return betMoney * ratioChange;
    }

    @Override
    protected void checkRemainCard(TienlenPlayer tienlenPlayer) {
        tienlenPlayer.checkRemainCard();
    }

    @Override
    public List<Integer> getAutoFightCard() {
        List<Integer> listCards = new LinkedList<Integer>();
        int cardChoose = listPlayers[gameTurn].getSugesstMinCard();
        listCards.add(cardChoose);
        return listCards;
    }

    @Override
    public void doLoop() {
    }

    @Override
    public void doReady() {
        setIsHostTimeOut(true);
        tienlenResponse.sendTimeCount(tableHost);
    }

    @Override
    public void updateNumberPlaying() {
        numberPlayings = 0;
        for (int i = 0; i < maxPlayer; i++) {
            if (listPlayers[i] != null) {
                numberPlayings++;
            }
        }
    }
}
